package com.fzkj.juc.spinlock;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @DESCRIPTION 自己手写一个自旋锁的实现
 */
public class SpinLock {

    private AtomicReference<Thread> reference = new AtomicReference<>();

    public void lock(){
        // 获取当前线程
        Thread currentThread = Thread.currentThread();
        while (!reference.compareAndSet(null, currentThread)){}
        System.out.println(currentThread.getName() + "\t is coming");
    }

    public void unLock(){
        Thread currentThread = Thread.currentThread();
        reference.compareAndSet(currentThread, null);
        System.out.println(currentThread.getName() + "\t 释放锁");
    }

}
